import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertWeaponsMessage {
    public static void main(String[] args) {
        // MySQL数据库连接参数
        String url = "jdbc:mysql://localhost:3306/game_message"; // 根据实际情况修改URL
        String username = "root";
        String password = "your_password_for_mysql"; // 数据库密码
        
        // 插入或更新数据的变量
        String tableName = "localmessage"; // 表名
        String userName = "SJJ"; // 用户名
        String weaponNames = "A2,B1,C1,D1,E1,F1,G1"; // 武器名称
        int gameTime = 3000; // 游戏时间
        String clearanceStatus = "1 1 0 0"; // 完成状态
        String clearanceTime = "1500 1400 -1 -1"; // 清关时间
        
        // SQL插入或更新语句
        String sql = "INSERT INTO " + tableName + " (username, weapon_names, gametime, clearancestatus, clearancetime) "
                   + "VALUES (?, ?, ?, ?, ?) "
                   + "ON DUPLICATE KEY UPDATE weapon_names = ?, gametime = ?, clearancestatus = ?, clearancetime = ?";
        
        // 创建数据库连接
        try (Connection connection = DriverManager.getConnection(url, username, password);
             PreparedStatement statement = connection.prepareStatement(sql)) {
            
            // 设置插入数据的参数
            statement.setString(1, userName);
            statement.setString(2, weaponNames);
            statement.setInt(3, gameTime);
            statement.setString(4, clearanceStatus);
            statement.setString(5, clearanceTime);
            
            // 设置更新数据的参数（当用户存在时）
            statement.setString(6, weaponNames);
            statement.setInt(7, gameTime);
            statement.setString(8, clearanceStatus);
            statement.setString(9, clearanceTime);
            
            // 执行插入或更新操作
            int rowsInsertedOrUpdated = statement.executeUpdate();
            if (rowsInsertedOrUpdated > 0) {
                System.out.println("数据插入或更新成功！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
